package cn.tedu.yhg.good.service.impl;


import cn.tedu.yhg.good.mapper.GoodMapper;
import cn.tedu.yhg.good.pojo.dto.GoodQuery;
import cn.tedu.yhg.good.pojo.dto.GoodSaveParam;
import cn.tedu.yhg.good.pojo.entity.Good;
import cn.tedu.yhg.good.pojo.property.BaseInfoTitleAndSpecific;
import cn.tedu.yhg.good.pojo.property.Detail;
import cn.tedu.yhg.good.pojo.vo.GoodByteVO;
import cn.tedu.yhg.good.pojo.vo.GoodVO;
import cn.tedu.yhg.good.service.GoodService;
import cn.tedu.yhg.tool.GoodFieldTransferDatabase;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.Arrays;
import java.util.List;


@Service
public class GoodServiceImpl implements GoodService {
    @Autowired
    private GoodMapper goodMapper;

    @Override
    public void save(GoodSaveParam goodSaveParam) {

        Good good = new Good();

        System.out.println("goodSaveParam = " + goodSaveParam);
        BeanUtils.copyProperties(goodSaveParam, good);
        byte[] goodImgSerialize = GoodFieldTransferDatabase.serialize(goodSaveParam.getGoodImg(), byte[].class);
        byte[] baseInfoTitleAndSpecificSerialize = GoodFieldTransferDatabase.serialize(goodSaveParam.getBaseInfoTitleAndSpecific(), byte[].class);
        byte[] detailsSerialize = GoodFieldTransferDatabase.serialize(goodSaveParam.getDetails(), byte[].class);
        good.setGoodImg(goodImgSerialize);
        good.setBaseInfoTitleAndSpecific(baseInfoTitleAndSpecificSerialize);
        good.setDetails(detailsSerialize);
        goodMapper.save(good);



/*

        try {
            *//* 思路:ObjectOutputStream实现流转换,ByteArrayOutputStream仅是为了字节传输的容器*//*

            *//* 1.创建对象流所对应的容器
            创建一个 ByteArrayOutputStream 实例 baos。这个对象提供了一个内存中
           的缓冲区，你可以将数据写入这个缓冲区，并随后通过调用 toByteArray() 方法来
           获取这个缓冲区中的数据（作为一个字节数组）。*//*
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            *//*2.创建对象流并传入对象
            使用 baos 作为参数来创建一个 ObjectOutputStream 实例 oos。现在，oos 就是一个可
            以序列化对象的流，它会将序列化的数据写入 baos 提供的缓冲区。*//*
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            *//*3.对象序列化,此时在容器内已经变为字节流
            调用 oos.writeObject(data) 来序列化对象 data。这里假设 data 是一个实现了 Serializable 接
            口的对象。序列化过程会将对象的状态转换为字节，并将这些字节写入 baos 提供的缓冲区。*//*
            oos.writeObject(goodSaveParam.getGoodImg());
            *//*4.关闭对象流
            调用 oos.close() 来关闭 ObjectOutputStream。关闭流是一个好习惯，因为它会确保所有待写入的数据
            都被刷新到底层流（在这个例子中是 baos），并释放与流关联的任何系统资源。*//*
            oos.close();
            *//*5.字节流容器中的字节流转字节
            调用 baos.toByteArray() 来获取 baos 缓冲区中的数据，并将其作为字节数组返回。这个字节数组包
            含了序列化后的对象数据。*//*
            good.setGoodImg(baos.toByteArray());
            // 调试信息：查看序列化后的字节数组
            System.out.println("Serialized goodImg: " + Arrays.toString(good.getGoodImg()));

//            good.setBaseInfoTitleAndSpecific(null);


            ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
            ObjectOutputStream oos2 = new ObjectOutputStream(baos2);
            oos2.writeObject(goodSaveParam.getBaseInfoTitleAndSpecific());
            oos2.close();
            good.setBaseInfoTitleAndSpecific(baos2.toByteArray());


//            good.setDetails(null);


            ByteArrayOutputStream baos3 = new ByteArrayOutputStream();
            ObjectOutputStream oos3 = new ObjectOutputStream(baos3);
            oos3.writeObject(goodSaveParam.getDetails());
            oos3.close();
            good.setDetails(baos3.toByteArray());


            goodMapper.save(good);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

*//*        try {

            System.out.println(Arrays.toString(good.getGoodImg()));
            GoodVO goodVO = new GoodVO();
            ByteArrayInputStream bais = new ByteArrayInputStream(good.getGoodImg());
            ObjectInputStream ois = new ObjectInputStream(bais);
            try {
                List<String> goodImgVO=(List<String>)ois.readObject();
                goodVO.setGoodImg(goodImgVO);
                System.out.println("select =12131321313 "+goodVO);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }*/

    }

    public GoodVO select(GoodQuery goodQuery) {
        GoodVO goodVO = new GoodVO();
/*        try {

            GoodByteVO goodByteVO = goodMapper.select(goodQuery);
            byte[] goodImgByte = goodByteVO.getGoodImg();
            byte[] baseInfoTitleAndSpecificByte = goodByteVO.getBaseInfoTitleAndSpecific();
            byte[] detailsByte = goodByteVO.getDetails();
            ByteArrayInputStream bais1 = new ByteArrayInputStream(goodImgByte);
            ObjectInputStream ois1 = new ObjectInputStream(bais1);
            ByteArrayInputStream bais2 = new ByteArrayInputStream(baseInfoTitleAndSpecificByte);
            ObjectInputStream ois2 = new ObjectInputStream(bais2);
            ByteArrayInputStream bais3 = new ByteArrayInputStream(detailsByte);
            ObjectInputStream ois3 = new ObjectInputStream(bais3);
            try {
                List<String> goodImgVO = (List<String>) ois1.readObject();
                ois1.close();
                List<BaseInfoTitleAndSpecific> baseInfoTitleAndSpecific = (List<BaseInfoTitleAndSpecific>) ois2.readObject();
                ois2.close();
                List<Detail> details = (List<Detail>) ois3.readObject();
                ois3.close();
                goodVO.setGoodImg(goodImgVO);
                goodVO.setBaseInfoTitleAndSpecific(baseInfoTitleAndSpecific);
                goodVO.setDetails(details);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }*/
        GoodByteVO goodByteVO = goodMapper.select(goodQuery);
        byte[] goodImgByte = goodByteVO.getGoodImg();
        byte[] baseInfoTitleAndSpecificByte = goodByteVO.getBaseInfoTitleAndSpecific();
        byte[] detailsByte = goodByteVO.getDetails();
        List<String> goodImgByteDeserialize = GoodFieldTransferDatabase.deserialize(goodImgByte, String.class);
        List<BaseInfoTitleAndSpecific> baseInfoTitleAndSpecificDeserialize = GoodFieldTransferDatabase.deserialize(baseInfoTitleAndSpecificByte, BaseInfoTitleAndSpecific.class);
        List<Detail> detailDeserialize = GoodFieldTransferDatabase.deserialize(detailsByte, Detail.class);
        goodVO.setGoodImg(goodImgByteDeserialize);
        goodVO.setBaseInfoTitleAndSpecific(baseInfoTitleAndSpecificDeserialize);
        goodVO.setDetails(detailDeserialize);
        return goodVO;
    }
}
